#include "sound_output.h"

pa_simple			*initserver(pa_sample_format_t	format,
					    int			rate,
					    int			channels)
{
  pa_simple			*s = NULL;
  int				error;

  const pa_sample_spec	ss =
    {
      .format = format,
      .rate = rate,
      .channels = channels
    };

  /* Create a new playback stream */
  if (!(s = pa_simple_new(NULL, "aavp", PA_STREAM_PLAYBACK,
			  NULL, "flux audio", &ss, NULL, NULL, &error)))
  {
    fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
    if (s)
      pa_simple_free(s);
  }

  return (s);
}


int			sendserver(pa_simple			*s,
				   int16_t			*data,
				   int				buf_size)
{
  pa_usec_t		latency;
  int			error;

  /* int16_t		data[BUFSIZE]; */
  /* audio		a; */
  /* int		writt = 2; */
  /* int		i; */


  /* for (i = 0; i < buf_size/writt ; i++) */
  /*   { */
  /* fin buffer sortie boucle  */

  /*check latency*/
  if ((latency = pa_simple_get_latency(s, &error)) == (pa_usec_t) - 1)
  {
    fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
    /* if (s) */
    /*   pa_simple_free(s); */
    return (-1);
  }
  /*      fprintf(stderr, "%0.0f usec    \r", (float)latency);*/

  /* ... and play it */
  if (pa_simple_write(s, data, (size_t) buf_size, &error) < 0)
  {
    fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
    /* if (s) */
          /*   pa_simple_free(s); */
    return (-1);
  }
  /* } */
  return (0);
}


/* int		end_pulse(s) */
/* { */
  /* /\* Make sure that every single sample was played *\/ */
  /* if (pa_simple_drain(s, &error) < 0) */
  /*   { */
  /*     fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error)); */
  /*     /\* if (s) *\/ */
  /*     /\* 	pa_simple_free(s); *\/ */
  /*     return -1; */
  /*   } */

  /*the server play the sound automatically*/

  /* if (s) */
  /*   pa_simple_free(s); */

/* } */

